Ext.define('Zm.controller.Property', {
    extend: 'Ext.app.Controller',
    uses:['Zm.Common','Zm.i18n.Lang','Zm.model.Pointset'],
	stores:['DevAttr','PortInfo','Pointset'],
    refs:[
        {ref:'X3dProp', selector:'x3dproperty'},
        {ref:'LineProp', selector:'lineproperty'},
        {ref:'PointProp', selector:'linepointset'},
        {ref:'X3dView', selector:'x3dview'}
    ],
    init: function(){
        var me = this;
        me.control({
            'x3dview':{
                poschange:me.onPosChange
            },
            'x3dproperty':{
                itemdblclick:me.onItemDblClick,
                propertychange:me.onEdit
            },
            'lineproperty':{
                propertychange:me.onEditLine,
                itemdblclick:me.onLineClick  
            },
            'lineproperty button[action=add]':{
                click:me.onAddLine
            },
            'lineproperty button[action=del]':{
                click:me.onDelLine
            },
            'linepointset':{
                itemdblclick:me.onEditPoint
            },
            'linepointset button[action=add]':{
                click:me.onAddPoint
            },
            'linepointset button[action=del]':{
                click:me.onDelPoint
            },
            'linepointset button[action=finish]':{
                click:me.onPointDone
            }
        });
	},
//    onDevClick:function(view,devid,element,event){
//        var me = this,
//            x3dProp = me.getX3dProp();
//        x3dProp.setValue(devid);
//    },    
    onEdit:function(view,editor,e,eOpt){
        var me = this,rawValue,
            record = e.record,
            vtid = record.get('vtid');
        if(e.originalValue != e.value){//仅修改后的递交
            if(e.colIdx ==1){
                rawValue = view.editors.get(vtid).field.rawValue;
                record.set('valdesc',rawValue);
                Ext.Ajax.request({
                    url: g_app + '/Dev/setAttr',
                    params: {devid: record.get('ddevid'),attrid:record.get('attrid'),val:record.get('defval')},
                    scope: me,
                    success:function(response){
                        var obj = Ext.decode(response.responseText,true),
                            x3dview = me.getX3dView(),
                            transNode;
                        if(obj==null) return;
                        if(obj.success){
                            if(obj.data){
                                x3dview.transDev(obj.data.devid,obj.data);
                            }
                        }
                    },
                    failure:Zm.Common.onAFail
                });
            }
        }
        record.commit();
    },
    onAddLine:function(btn){
        var me = this,
            prop = btn.up('lineproperty'),
            rd = Ext.create('Zm.model.PortInfo');
        prop.store.add(rd);
    },
    onDelLine:function(btn){
        var me = this,cid,
            lang = Zm.i18n.Lang,
            prop = btn.up('lineproperty'),
            sm = prop.getSelectionModel(),
            records = sm.getSelection();
        if(records.length>0){
            cid = records[0].get('cid');
            Ext.MessageBox.show({
               title:lang.tDel,
               msg: Ext.String.format(lang.tDelLineTip,records[0].get('devname')),
               buttons: Ext.Msg.YESNO,
               fn: me.onDelCallback,
               scope: me,
               cid:cid,
               icon: Ext.Msg.QUESTION
           });
        }
    },
    onDelCallback: function(btn,text,opt){
        var me = this,
            lang = Zm.i18n.Lang,
            cm = Zm.Common;
        if(btn == 'yes'){
            Ext.Ajax.request({
                url: g_app + '/Lines/del',
                params: {cid: opt.cid},
                scope: me,
                success:function(response,options){ 
                     var obj = Ext.decode(response.responseText,true);
                     if(obj==null) return;
                     if(obj.success){
                         me.getLineProp().store.load();                            
                     }else{
                         cm.errMsg(lang.tFail, obj.msg); 
                     }
                },
                failure:cm.onAFail
             });    
        }
    },    
    onEditLine:function(view,editor,e,eOpt){
        var me = this,rawValue,sdevid,devid,sx,dx,
            record = e.record,
            itemId = e.column.getItemId(),
            prop = me.getLineProp(),
            x3dview = me.getX3dView(),
            cid = record.get('cid');
        if(e.originalValue != e.value){//仅修改后的递交
            if(e.colIdx ==1){
                rawValue = editor.editors.getByKey(itemId).field.rawValue;
                record.set('devname',rawValue);
                sdevid = prop.devid;
                devid = record.get('devid');
                Ext.Ajax.request({
                    url: g_app + '/Lines/save',
                    params: {cid:cid,sdevid: sdevid,devid:devid},
                    scope: me,
                    success:function(response){
                        prop.setValue(sdevid);
                    },
                    failure:Zm.Common.onAFail
                });
            }
        }
        record.commit();
    },
    onItemDblClick:function(view, record, item, index, e, eOpts){
        var me = this,
            devid = record.get('devid'),
            editor = record.get('editor');
        if(editor==='devfield'){
            me.getX3dView().goDev(devid); 
            me.getLineProp().setValue(devid);
        }
    },
    onLineClick:function(view, record, item, index, e, eOpts){
        var me = this,
            lineid = record.get('lineid');
        me.getPointProp().setValue(lineid);
    },
    onAddPoint:function(btn){
        var me = this,xyz,
            cm = Zm.Common,
            x3dview = me.getX3dView(),
            prop = btn.up('linepointset'),
            num = prop.store.count();
        if(num>0){
            xyz = prop.store.getAt(num-1).get('xyz');
        }else{
            xyz = x3dview.originalVP.position;
        }
        Ext.Ajax.request({
            url: g_app + '/Lines/addPoint',
            params: {lineid: prop.lineid,xyz:xyz},
            scope: me,
            success:function(response,options){ 
                 var obj = Ext.decode(response.responseText,true);
                 if(obj==null) return;
                 if(obj.success){
                     prop.setValue(obj.data.lineid);
                     x3dview.refLines(obj.data.lineid);
                 }
            },
            failure:cm.onAFail
         });    

    },
    onDelPoint:function(btn){
        var me = this,cid,
            lang = Zm.i18n.Lang,
            cm = Zm.Common,
            x3dview = me.getX3dView(),
            prop = btn.up('linepointset'),
            sm = prop.getSelectionModel(),
            records = sm.getSelection();
        if(records.length>0){
            cid = records[0].get('lid');
            Ext.Ajax.request({
                url: g_app + '/Lines/delPoint',
                params: {lineid: prop.lineid,lid:cid},
                scope: me,
                success:function(response,options){ 
                     var obj = Ext.decode(response.responseText,true);
                     if(obj==null) return;
                     if(obj.success){
                         prop.setValue(obj.data.lineid);
                         x3dview.refLines(obj.data.lineid);
                     }
                },
                failure:cm.onAFail
             });    
        }
    },
    onEditPoint:function(view, record, item, index, e, eOpts){
        var me = this,i,
            x3dview = me.getX3dView(),
            objPos = x3dview.objPosTran,
            prop = me.getPointProp(),
            devs = new Array();
            lid = record.get('lid');
        prop.editLid(lid);
        objPos.setAttribute('render','true');
        objPos.setAttribute('translation',record.get('xyz'));
        for(i=0;i<prop.devs.length;i++){
            devs[i] = prop.devs[i].devid;
        }
        x3dview.showOnly(devs);
    },
    onPointDone:function(btn){
        var me = this,
            x3dview = me.getX3dView(),
            objPos = x3dview.objPosTran,
            cm = Zm.Common,
            prop = me.getPointProp(),
            record = prop.store.findRecord('lid',prop.eid);
        if(record){
            Ext.Ajax.request({
                url: g_app + '/Lines/savePoint',
                params: {lineid:record.get('lineid'),lid: prop.eid,xyz:record.get('xyz')},
                scope: me,
                success:function(response,options){ 
                     var obj = Ext.decode(response.responseText,true);
                     if(obj==null) return;
                     if(obj.success){
                         prop.setValue(obj.data.lineid);
                         x3dview.refLines(obj.data.lineid);
                         prop.down('button[action=finish]').setDisabled(true);
                         x3dview.objPosTran.setAttribute('render',false);
                         x3dview.showAll();
                     }
                },
                failure:cm.onAFail
             });    
        }
    },
    onPosChange:function(view,xyz){
        var me = this,linedata,i,xyzs = new Array(),
            x3dview = me.getX3dView(),
            prop = me.getPointProp(),
            rds = prop.store.getRange(),
            lineid = prop.lineid;
        prop.updatePoint(xyz);
        x3dview.delLine(lineid);
        for(i=0;i<rds.length;i++){
            xyzs[i] = {xyz:rds[i].get('xyz')};
        }
        linedata = {xyz:xyzs,devs:prop.devs,lineid:lineid,linetype:prop.linetype};
        x3dview.addLine(linedata);
    }
});